<?php

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/ws.php';
if (file_exists($cron_lang))
{
    global $_LANG;

    include_once($cron_lang);
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'ws_desc';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = '';

    /* 版本号 */
    $modules[$i]['version'] = '1.0.0';

    /* 配置信息 */
    $modules[$i]['config']  = '';

    return;
}

$num = 100;

// $where =  "WHERE oi.order_status=".OS_CONFIRMED;
// $where .= " AND oi.pay_status=".PS_PAYED;
// $where .= " AND oi.push_status=".PUS_NOTPUSH;
// $where .= " AND oi.shipping_status=".SS_UNSHIPPED;

// $sql = "SELECT COUNT(*) FROM ".
// 		$ecs->table('order_info')." AS oi ".
// 		$where;

// $total = $db->getOne($sql);

// while ($total>0)
// {
// 	push($limit);
// 	$total -= $limit;
// }

$where =  "WHERE oi.order_status=".OS_CONFIRMED;
$where .= " AND oi.pay_status=".PS_PAYED;
$where .= " AND oi.push_status=".PUS_NOTPUSH;
$where .= " AND oi.shipping_status=".SS_UNSHIPPED;
$where .= " AND og.suppliers_id=1"; //威时管理的仓库

// $limit = " LIMIT ".$num;

$orderBy = " ORDER BY oi.add_time DESC ";

$sql = "SELECT oi.*,og.goods_number,og.goods_price,og.goods_sn,og.goods_name,".
		"(og.goods_price-og.goods_tax) * og.goods_number AS goods_total,og.goods_tax FROM ".
		$GLOBALS['ecs']->table('order_info')." AS oi ".
		" JOIN ".$GLOBALS['ecs']->table('order_goods_push')." AS og ON og.order_id=oi.order_id ".
		$where.$orderBy.$limit;

$goods = $GLOBALS['db']->getAll($sql);
// print_r($goods);
// echo count($goods);
if (empty($goods)) return;
$temp = array();
foreach ($goods as $key=>$row)
{
	if (!empty($row['pp_sn'])){
		$temp[$row['order_id']]['orderId'] = $row['pp_sn'];
		$temp[$row['order_id']]['orderDate'] = local_date("Y-m-d H:i:s",$row['add_time']);
		$temp[$row['order_id']]['name'] = $row['consignee'];
		$temp[$row['order_id']]['province'] = qin_get_region($row['province']);
		$temp[$row['order_id']]['city'] = qin_get_region($row['city']);
		$temp[$row['order_id']]['area'] = qin_get_region($row['district']);
		$temp[$row['order_id']]['addr'] = qin_get_region($row['province']).qin_get_region($row['city']).qin_get_region($row['district']).$row['address'];
		$temp[$row['order_id']]['zipCode'] = $row['zipcode'];
		$temp[$row['order_id']]['cardType'] = 0;
		/* 消费者信息 */
// 		$temp[$row['order_id']]['cardNo'] = $row['buyer_credit_card'];
// 		$temp[$row['order_id']]['consumerName'] = $row['buyer_name'];
// 		$temp[$row['order_id']]['consumerPhone'] = $row['buyer_phone'];
// 		$temp[$row['order_id']]['consumerProvince'] = qin_get_region($row['buyer_province']);
// 		$temp[$row['order_id']]['consumerCity'] = qin_get_region($row['buyer_city']);
// 		$temp[$row['order_id']]['consumerArea'] = qin_get_region($row['buyer_district']);
// 		$temp[$row['order_id']]['consumerAddr'] = qin_get_region($row['buyer_province']).qin_get_region($row['buyer_city']).qin_get_region($row['buyer_district']).$row['buyer_address'];

		$temp[$row['order_id']]['cardNo'] = $row['credit_card'];
		$temp[$row['order_id']]['consumerName'] = $temp[$row['order_id']]['name'];
		$temp[$row['order_id']]['consumerPhone'] = $row['mobile'];
		$temp[$row['order_id']]['consumerProvince'] = $temp[$row['order_id']]['province'];
		$temp[$row['order_id']]['consumerCity'] = $temp[$row['order_id']]['city'];
		$temp[$row['order_id']]['consumerArea'] = $temp[$row['order_id']]['area'];
		$temp[$row['order_id']]['consumerAddr'] = $temp[$row['order_id']]['addr'];
		
		$temp[$row['order_id']]['phone'] = $row['mobile'];
// 		$temp[$row['order_id']]['mobile'] = $row['tel'];
		$temp[$row['order_id']]['invoiceHead'] = null;
		$temp[$row['order_id']]['invoiceType'] = null;
		$temp[$row['order_id']]['invoice'] = null;
		$temp[$row['order_id']]['total'] += floatval($row['goods_total']);
		$temp[$row['order_id']]['freight'] = floatval($row['ws_push_diff']);
		$temp[$row['order_id']]['insuredFee'] = floatval($row['insure_fee']);
		$temp[$row['order_id']]['seller'] = $_CFG['seller'];
		$temp[$row['order_id']]['notes'] = null;
		$temp[$row['order_id']]['portCode'] = $_CFG['portCode'];
		$temp[$row['order_id']]['ieFlag'] = $_CFG['ieFlag'];
		$temp[$row['order_id']]['busiMode'] = $_CFG['busiMode'];
		$temp[$row['order_id']]['pickMode'] = intval($_CFG['pickMode']);
		$temp[$row['order_id']]['whCode'] = $_CFG['whCode'];
		$temp[$row['order_id']]['expressCode'] = $_CFG['expressCode'];
		$temp[$row['order_id']]['expressNo'] = null;
		$temp[$row['order_id']]['destCity'] = null;
		$temp[$row['order_id']]['prodFlag'] = null;
		$temp[$row['order_id']]['senderContact'] = $_CFG['senderContact'];
		$temp[$row['order_id']]['senderPhone'] = $_CFG['senderPhone'];
		$temp[$row['order_id']]['platformCode'] = $_CFG['platformCode'];
		$temp[$row['order_id']]['paymentCode'] = $_CFG['paymentCode'];
		$temp[$row['order_id']]['isgac'] = intval($_CFG['isgac']);
		$temp[$row['order_id']]['extraInfoList'] = null;
		
		$good['productId'] = substr($row['goods_sn'],3);
		// 	$good['productId'] = '8712400110136';
		$good['productName'] = str_replace(array('#','+','&'),' ',$row['goods_name']);
		$good['qty'] = intval($row['goods_number']);
		$good['price'] = floatval($row['goods_price'])-floatval($row['goods_tax']);
		$good['total'] = floatval($row['goods_total']);
// 		$good['isGroup'] = 'N';
// 		$good['groupBarcode'] = null;
		$good['postTax'] = intval($row['goods_number'])*floatval($row['goods_tax']);
		
		$temp[$row['order_id']]['tax'] += $good['postTax'];
		
		$temp[$row['order_id']]['orderItemList']['orderItems'][] = $good;
	}
	
}
// print_r($temp);
$total = count($temp);
if ($total>0){
	$snArr = array();
	foreach ($temp as $k=>$v)
	{
	
		$snArr[] = $v['orderId'];
	}
	
	$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET push_status=".PUS_PUSHED." WHERE pp_sn IN(".implode(',', $snArr).")";
	$GLOBALS['db']->query($sql);
	
	for ($i=0;$i<$total;$i+=$num){
		$slice = array_slice($temp, $i, $num);
		push2($slice);
		// 	print_r($slice);
	
	}
}

function push2($slice)
{
	require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/wtd.php');
	
	$snArr = array();
	foreach ($slice as $k=>$v)
	{
		$data['orders'][] = $v;
		$snArr[] = get_order_id_by_pp($v['orderId']);
	}
	$result = add_order_batch($data);
// 	print_r($result);
	/* [code] => 0
	 [desc] => 操作失败
	 [errorInfoList] => Array (
	 [errorInfos] => Array (
	 [0] => Array (
	 [errCode] => 124
	 [errPath] =>
	 [errMsg] => 订单 2016011119684 商品VMDA934983875 不存在有效的企业商品信息记录
	 [errKeyCode] => 2016011119684
	 [errChildKeyCode] => VMDA934983875
	 )
	 [1] => Array (
	 [errCode] => 124
	 [errPath] =>
	 [errMsg] => 订单 2016011176853 商品VMDA934983875 不存在有效的企业商品信息记录
	 [errKeyCode] => 2016011176853
	 [errChildKeyCode] => VMDA934983875
	 )
	 )
	 )
	 [updateCount] => 0
	[errorCount] => 0 */
	
	if ($result['code']==1)
	{
		foreach ($snArr as $k=>$v)
		{
			push_log($v,$result['code'],$result['desc'],$result['errMsg'],$result['updateCount'],$result['errorCount'],1);
		}
		
	
	}
	elseif ($result['errorInfoList']['errorInfos'])
	{
		foreach ($result['errorInfoList']['errorInfos'] as $key=>$info)
		{
			
			
			if ($info['errKeyCode'])
			{
				$order_id = get_order_id_by_pp($info['errKeyCode']);
				push_log($order_id,$info['errCode'],$result['desc'],$info['errMsg'],$result['updateCount'],$result['errorCount'],1);
				$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET push_status=".PUS_PUSHFAIL." WHERE pp_sn='$info[errKeyCode]'";
			}
			else
			{
				foreach ($snArr as $k=>$v)
				{
					push_log($v,$info['errCode'],$result['desc'],$info['errMsg'],$result['updateCount'],$result['errorCount'],1);
				}
				$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET push_status=".PUS_PUSHFAIL." WHERE order_id IN(".implode(',', $snArr).")";
			}
		
			$GLOBALS['db']->query($sql);
		}
		
	}
	else
	{
		foreach ($snArr as $k=>$v)
		{
			push_log($v,$result['code'],$result['desc'],$result['errMsg'],$result['updateCount'],$result['errorCount'],1);
			
		}
		$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET push_status=".PUS_PUSHFAIL." WHERE order_id IN(".implode(',', $snArr).")";
		$GLOBALS['db']->query($sql);
		
	}
	

}





?>